package ibm.cognos.imt.template

import ibm.cognos.imt.chart.CombinationChart
import groovy.xml.MarkupBuilder

/**
 * Created with IntelliJ IDEA.
 * User: eric
 * Date: 5/16/14
 * Time: 11:15 AM
 * To change this template use File | Settings | File Templates.
 */
class CombinationChartTemplate extends AbstractTemplate{
    CombinationChart chart
    CombinationChartTemplate(CombinationChart combinationChart) {
        chart = combinationChart
    }

    @Override
    def generateTemplateSpec(MarkupBuilder builder) {
        builder.combinationChart(showTooltips: "true", maxHotspots: "100000", refQuery: "${chart.queryName}", name: "${chart.name}") {
            legend {
                legendPosition {
                    relativePosition()
                }
                legendTitle(refQuery: "${chart.queryName}") {
                    style {
                        defaultStyles {
                            defaultStyle(refStyle: "lx")
                        }
                    }
                }
                style {
                    defaultStyles {
                        defaultStyle(refStyle: "lg")
                    }
                }
            }
            ordinalAxis {
                axisTitle(refQuery: "${chart.queryName}") {
                    style {
                        defaultStyles {
                            defaultStyle(refStyle: "at")
                        }
                    }
                }
                axisLine(color: "black")
                style {
                    defaultStyles {
                        defaultStyle(refStyle: "al")
                    }
                }
            }
            numericalAxisY1 {
                axisTitle(refQuery: "${chart.queryName}") {
                    style {
                        defaultStyles {
                            defaultStyle(refStyle: "at")
                        }
                    }
                }
                gridlines(color: "#cccccc")
                axisLine(color: "black")
                style {
                    defaultStyles {
                        defaultStyle(refStyle: "al")
                    }
                }
            }
            combinationChartTypes {
                if ({chart.slices} != null) {
                    bar {
                        chartNodes {

                            chartNode {
                                chartNodeMembers {
                                    chartNodeMember(refDataItem: "${chart.slices}") {
                                        chartContents {
                                            chartTextItem {
                                                dataSource {
                                                    memberCaption()
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                } else {

                    bar()
                }
            }
            style {
                defaultStyles {
                    defaultStyle(refStyle: "ch")
                }
            }
            commonClusters {
                chartNodes {
                    chartNode {
                        chartNodeMembers {
                            chartNodeMember(refDataItem: "${chart.categories}") {
                                chartContents {
                                    chartTextItem {
                                        dataSource {
                                            memberCaption()
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            defaultChartMeasure(refDataItem: "${chart.measure}")
        }
    }

    @Override
    def getTemplateNode() {
        root = new XmlSlurper().parseText(getSpecStr())
        root
    }
}
